package com.mini.grpc.protoc.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 服务定义模型
 * 表示一个gRPC服务的完整定义
 * 
 * @author Mini-gRPC
 */
public class ServiceDefinition {
    
    private final String packageName;
    private final String serviceName;
    private final String javaPackage;
    private final String outerClassName;
    private final List<MethodDefinition> methods;
    private final List<String> imports;
    
    /**
     * 构造函数
     * 
     * @param packageName 包名
     * @param serviceName 服务名
     * @param javaPackage Java包名
     * @param outerClassName 外部类名
     * @param methods 方法列表
     * @param imports 导入列表
     */
    public ServiceDefinition(String packageName, 
                           String serviceName,
                           String javaPackage,
                           String outerClassName,
                           List<MethodDefinition> methods,
                           List<String> imports) {
        this.packageName = packageName;
        this.serviceName = serviceName;
        this.javaPackage = javaPackage;
        this.outerClassName = outerClassName;
        this.methods = Collections.unmodifiableList(new ArrayList<>(methods));
        this.imports = Collections.unmodifiableList(new ArrayList<>(imports));
    }
    
    /**
     * 获取包名
     * 
     * @return 包名
     */
    public String getPackageName() {
        return packageName;
    }
    
    /**
     * 获取服务名
     * 
     * @return 服务名
     */
    public String getServiceName() {
        return serviceName;
    }
    
    /**
     * 获取Java包名
     * 
     * @return Java包名
     */
    public String getJavaPackage() {
        return javaPackage;
    }
    
    /**
     * 获取外部类名
     * 
     * @return 外部类名
     */
    public String getOuterClassName() {
        return outerClassName;
    }
    
    /**
     * 获取方法列表
     * 
     * @return 方法列表
     */
    public List<MethodDefinition> getMethods() {
        return methods;
    }
    
    /**
     * 获取导入列表
     * 
     * @return 导入列表
     */
    public List<String> getImports() {
        return imports;
    }
    
    /**
     * 获取服务的完全限定名
     * 
     * @return 完全限定名
     */
    public String getFullServiceName() {
        if (packageName != null && !packageName.isEmpty()) {
            return packageName + "." + serviceName;
        }
        return serviceName;
    }
    
    /**
     * 获取生成的Java类名
     * 
     * @return Java类名
     */
    public String getJavaClassName() {
        return serviceName + "Grpc";
    }
    
    /**
     * 获取服务接口名
     * 
     * @return 服务接口名
     */
    public String getServiceInterfaceName() {
        return serviceName;
    }
    
    /**
     * 获取阻塞存根类名
     * 
     * @return 阻塞存根类名
     */
    public String getBlockingStubClassName() {
        return serviceName + "BlockingStub";
    }
    
    /**
     * 获取异步存根类名
     * 
     * @return 异步存根类名
     */
    public String getAsyncStubClassName() {
        return serviceName + "Stub";
    }
    
    /**
     * 获取Future存根类名
     * 
     * @return Future存根类名
     */
    public String getFutureStubClassName() {
        return serviceName + "FutureStub";
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        ServiceDefinition that = (ServiceDefinition) obj;
        return Objects.equals(packageName, that.packageName) &&
               Objects.equals(serviceName, that.serviceName) &&
               Objects.equals(javaPackage, that.javaPackage) &&
               Objects.equals(outerClassName, that.outerClassName) &&
               Objects.equals(methods, that.methods) &&
               Objects.equals(imports, that.imports);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(packageName, serviceName, javaPackage, 
                          outerClassName, methods, imports);
    }
    
    @Override
    public String toString() {
        return "ServiceDefinition{" +
                "packageName='" + packageName + '\'' +
                ", serviceName='" + serviceName + '\'' +
                ", javaPackage='" + javaPackage + '\'' +
                ", outerClassName='" + outerClassName + '\'' +
                ", methods=" + methods.size() +
                ", imports=" + imports.size() +
                '}';
    }
    
    /**
     * 构建器类
     */
    public static class Builder {
        private String packageName;
        private String serviceName;
        private String javaPackage;
        private String outerClassName;
        private List<MethodDefinition> methods = new ArrayList<>();
        private List<String> imports = new ArrayList<>();
        
        /**
         * 设置包名
         * 
         * @param packageName 包名
         * @return 构建器实例
         */
        public Builder setPackageName(String packageName) {
            this.packageName = packageName;
            return this;
        }
        
        /**
         * 设置服务名
         * 
         * @param serviceName 服务名
         * @return 构建器实例
         */
        public Builder setServiceName(String serviceName) {
            this.serviceName = serviceName;
            return this;
        }
        
        /**
         * 设置Java包名
         * 
         * @param javaPackage Java包名
         * @return 构建器实例
         */
        public Builder setJavaPackage(String javaPackage) {
            this.javaPackage = javaPackage;
            return this;
        }
        
        /**
         * 设置外部类名
         * 
         * @param outerClassName 外部类名
         * @return 构建器实例
         */
        public Builder setOuterClassName(String outerClassName) {
            this.outerClassName = outerClassName;
            return this;
        }
        
        /**
         * 添加方法定义
         * 
         * @param method 方法定义
         * @return 构建器实例
         */
        public Builder addMethod(MethodDefinition method) {
            this.methods.add(method);
            return this;
        }
        
        /**
         * 添加导入
         * 
         * @param importName 导入名称
         * @return 构建器实例
         */
        public Builder addImport(String importName) {
            this.imports.add(importName);
            return this;
        }
        
        /**
         * 构建服务定义
         * 
         * @return 服务定义实例
         */
        public ServiceDefinition build() {
            if (serviceName == null || serviceName.trim().isEmpty()) {
                throw new IllegalArgumentException("serviceName cannot be null or empty");
            }
            
            return new ServiceDefinition(packageName, serviceName, javaPackage, 
                                       outerClassName, methods, imports);
        }
    }
} 